package com.zhiche.lisa.bms.controller.bill;


import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.dao.model.bill.Bill;
import com.zhiche.lisa.bms.dao.model.bill.BillIoDetail;
import com.zhiche.lisa.bms.dao.model.interfacelog.InterfaceLog;
import com.zhiche.lisa.bms.enums.BaseParamKeyEnum;
import com.zhiche.lisa.bms.pojo.vo.bill.*;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.service.base.BaseCommonParameService;
import com.zhiche.lisa.bms.service.bill.BillService;
import com.zhiche.lisa.bms.service.utils.InterfaceLogUtil;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.Account.AccountUtil;
import com.zhiche.lisa.core.utils.Account.JwtAccountVO;
import io.swagger.annotations.*;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 账单 前端控制器
 * </p>
 *
 * @author liuanshun
 * @since 2018-09-06
 */
@RestController
@RequestMapping("/bill")
@Api(value = "/bill", description = "账单管理接口", tags = {"账单管理接口"})
public class BillController {

    private static Logger LOGGER = LoggerFactory.getLogger(BillController.class);

    @Autowired
    private BillService billService;

    @Autowired
    private InterfaceLogUtil interfaceLogUtil;

    @Autowired
    private BaseCommonParameService baseCommonParameService;

    @PostMapping(value = "/queryBillIncomingPage")
    @ApiOperation(value = "获取应收出账列表", notes = "获取应收出账列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<BillVO>> queryBillIncomingPage(
            @ApiParam(name = "应收账单分页查询对象", value = "传入json格式以及查询条件", required = true)
            @RequestBody Page<Bill> inParam) {
        LOGGER.info("/BillController.queryBillIncomingPage Param {} ", inParam);
        RestfulResponse<Page<BillVO>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(billService.selectIncomingPage(inParam));
        } catch (Exception e) {
            LOGGER.error("/BillController.queryBillIncomingPage ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/queryBillPurchasePage")
    @ApiOperation(value = "获取应付出账列表", notes = "获取应付出账列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<BillVO>> queryBillPurchasePage(
            @ApiParam(name = "应付账单分页查询对象", value = "传入json格式以及查询条件", required = true)
            @RequestBody Page<Bill> inParam) {
        LOGGER.info("/BillController.queryBillPage Param {} ", inParam);
        RestfulResponse<Page<BillVO>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            pageConditionSet(inParam);
            restful.setData(billService.selectPurchasePage(inParam));
        } catch (BaseException e) {
            LOGGER.error("/BillController.queryBillPurchasePage ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.queryBillPurchasePage ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/queryBillPurchaseList")
    @ApiOperation(value = "根据支付申请ID，获取对应的应付出账", notes = "根据支付申请ID，获取对应的应付出账", httpMethod = "GET")
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillPayVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<BillPayVO>> queryBillPurchase(@RequestParam Long payId) {
        LOGGER.info("/BillController.queryBillPurchaseList Param {} ", payId);
        RestfulResponse<List<BillPayVO>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            // BillPayVO
            List<Bill> billList = billService.queryBillPurchaseList(payId);
            List<BillPayVO> voList = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(billList)) {
                billList.forEach(bill -> {
                    BillPayVO vo = new BillPayVO();
                    BeanUtils.copyProperties(bill, vo);
                    voList.add(vo);
                });
            }
            restful.setData(voList);
        } catch (BaseException e) {
            LOGGER.error("/BillController.queryBillPurchaseList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.queryBillPurchaseList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/queryReconciliationBill")
    @ApiOperation(value = "获取应收对账结果列表", notes = "获取应收对账结果列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillReconciliationVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<BillReconciliationVO>> queryReconciliationBill(
            @ApiParam(name = "对账分页信息查询对象", value = "传入json格式以及查询条件", required = true)
            @RequestBody Page inParam) {
        LOGGER.info("/BillController.queryBillPage Param {} ", inParam);
        RestfulResponse<Page<BillReconciliationVO>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(billService.queryReconciliationPage(inParam));
        } catch (BaseException e) {
            LOGGER.error("/BillController.queryReconciliationBill ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.queryReconciliationBill ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/queryReconciliationBillList")
    @ApiOperation(value = "获取应收对账结果列表", notes = "获取应收对账结果列表", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillReconciliationVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<BillReconciliationVO>> queryReconciliationBillList(
            @RequestParam("billId") @ApiParam(name = "对账分页信息查询对象", value = "传入json格式以及查询条件", required = true) Long billId) {
        LOGGER.info("/BillController.queryReconciliationBillList Param {} ", billId);
        RestfulResponse<List<BillReconciliationVO>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(billService.queryReconciliationList(billId));
        } catch (BaseException e) {
            LOGGER.error("/BillController.queryReconciliationBillList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.queryReconciliationBillList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/queryReceivableReconciliation")
    @ApiOperation(value = "获取应收对账结果列表", notes = "获取应收对账结果列表", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillReconciliationVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<BillReconciliationAndSunVO> queryReceivableReconciliation(@RequestBody CommonVO<BillIoDetail> param) {
        LOGGER.info("/BillController.queryReceivableReconciliation Param {} ", param);
        RestfulResponse<BillReconciliationAndSunVO> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(billService.queryReceivableReconciliation(param.getTypeVal(), param.getEntityParams()));
        } catch (BaseException e) {
            LOGGER.error("/BillController.queryReceivableReconciliation ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.queryReceivableReconciliation ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/queryPayableReconciliationBill")
    @ApiOperation(value = "获取应付对账结果列表", notes = "获取应付对账结果列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillReconciliationVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<BillReconciliationVO>> queryPayableReconciliationBill(
            @ApiParam(name = "批量结果数据", value = "批量结果数据", required = true)
            @RequestBody CommonVO<CustomerBillVO> commonVO) {
        LOGGER.info("/BillController.queryPayableReconciliationBill Param {} ", commonVO);
        RestfulResponse<List<BillReconciliationVO>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(billService.queryPayableReconciliationList(commonVO.getEntityParams(), commonVO.getTypeVal(), false, false));
        } catch (BaseException e) {
            LOGGER.error("/BillController.queryPayableReconciliationBill ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.queryPayableReconciliationBill ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/queryPayableReconciliationBillAll")
    @ApiOperation(value = "获取应付所有对账结果列表", notes = "获取应付所有对账结果列表", httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillReconciliationVO.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<BillReconciliationVO>> queryPayableReconciliationBillAll(
            @ApiParam(name = "批量结果数据", value = "批量结果数据", required = true)
            @RequestBody CommonVO<CustomerBillVO> commonVO) {
        LOGGER.info("/BillController.queryBillPage Param {} ", commonVO);
        RestfulResponse<List<BillReconciliationVO>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),
                CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(billService.queryPayableReconciliationList(commonVO.getEntityParams(),
                    commonVO.getTypeVal(), true, false));
        } catch (BaseException e) {
            LOGGER.error("/BillController.queryReconciliationBill ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.queryReconciliationBill ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/queryBillHeader")
    @ApiOperation(value = "获取对账结果头信息", notes = "获取对账结果头信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillHeaderVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<BillHeaderVO> queryBillHeader(@ApiParam(name = "billId", value = "传入客户账单id", required = true) String billId) {
        LOGGER.info("/BillController.queryBillHeader Param {} ", billId);
        RestfulResponse<BillHeaderVO> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(billService.queryBillHeader(billId));
        } catch (BaseException e) {
            LOGGER.error("/BillController.queryBillHeader ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.queryBillHeader ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/confirmCustomerBill")
    @ApiOperation(value = "客户账单确认", notes = "客户账单确认", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> confirmCustomerBill(@RequestBody Map<String, String> param) {
        LOGGER.info("/BillController.confirmCustomerBill Param {} ", param);
        RestfulResponse<String> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        InterfaceLog interfaceLog = new InterfaceLog();
        try {
            interfaceLogUtil.setCalleeCommonLog(interfaceLog, param, "/bill/confirmCustomerBill");
            String billCode = param.get("billCode");
            String checkCondition = param.get("checkCondition");
            if (Strings.isNullOrEmpty(billCode)) {
                throw new BaseException("请绑定账单号");
            }
            if (Strings.isNullOrEmpty(checkCondition)) {
                throw new BaseException("请绑定确认类型");
            }
            billService.updateBillAffirmState(billCode, Integer.valueOf(checkCondition));
            interfaceLogUtil.setSuccessLog(interfaceLog);
        } catch (BaseException e) {
            LOGGER.error("/BillController.confirmCustomerBill ERROR: {}", e.getMessage());
            interfaceLogUtil.setErrorLog(interfaceLog, e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.confirmCustomerBill ERROR: {}", e.getMessage());
            interfaceLogUtil.setErrorLog(interfaceLog, e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        // 保存日志
        interfaceLogUtil.saveInterfaceLog(interfaceLog);
        return restful;
    }

    @PostMapping(value = "/billErrorCallback")
    @ApiOperation(value = "账单异常回调", notes = "账单异常回调", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> billErrorCallback(@RequestBody CommonVO<BillReconciliationVO> billReconciliationVOCommonVO) {
        LOGGER.info("/BillController.billErrorCallback Param {} ", billReconciliationVOCommonVO);
        RestfulResponse<String> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            String billId = billReconciliationVOCommonVO.getTypeVal();
            billService.billErrorCallback(billId, billReconciliationVOCommonVO.getEntityParams());
        } catch (BaseException e) {
            LOGGER.error("/BillController.billErrorCallback ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.billErrorCallback ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/payableBillConfirm")
    @ApiOperation(value = "应付账单异常确认", notes = "应付账单异常确认", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> payableBillConfirm(@RequestParam("billId") String billId) {
        LOGGER.info("/BillController.payableBillConfirm Param {} ", billId);
        RestfulResponse<String> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            billService.payableBillConfirm(billId);
        } catch (BaseException e) {
            LOGGER.error("/BillController.payableBillConfirm ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.payableBillConfirm ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/payableBillException")
    @ApiOperation(value = "应付账单异常", notes = "应付账单异常", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> payableBillException(@RequestBody CommonVO<BillReconciliationVO> billReconciliationVOCommonVO) {
        LOGGER.info("/BillController.payableBillConfirm Param {} ", billReconciliationVOCommonVO);
        RestfulResponse<String> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            billService.payableBillException(billReconciliationVOCommonVO.getEntityParams(), billReconciliationVOCommonVO.getTypeVal());
        } catch (BaseException e) {
            LOGGER.error("/BillController.payableBillException ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.payableBillException ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/createBillByFeePayable")
    @ApiOperation(value = "根据账款生成账单", notes = "根据账款生成账单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<Bill>> createBillByFeePayable(@RequestBody CommonVO commonVO) {
        LOGGER.info("/BillController.createBillByFeePayable Param {} ", commonVO);
        RestfulResponse<List<Bill>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<Bill> bills = billService.createBillByFeePayable(Arrays.asList(commonVO.getIds()), commonVO.getTypeVal());
            restful.setData(bills);
        } catch (BaseException e) {
            LOGGER.error("/BillController.createBillByFeePayable ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.createBillByFeePayable ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/getBillIdToAnBilldFee/{id}")
    @ApiOperation(value = "根据账单ID得到账单和应收账款或应付账款信息", notes = "根据账单ID得到账单和应收账款或应付账款信息",
            httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillFeeDetailVO.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<BillFeeDetailVO> getBillIdToAnBilldFee(@PathVariable("id") String id) {
        LOGGER.info("/BillController.getBillIdToAnBilldFee Param {} ", id);
        RestfulResponse<BillFeeDetailVO> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            BillFeeDetailVO resultData = billService.getBillIdToAnBilldFee(id);
            restful.setData(resultData);
        } catch (BaseException e) {
            LOGGER.error("/BillController.getBillIdToAnBilldFee ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillController.getBillIdToAnBilldFee ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    /**
     * 批量确认账单
     *
     * @param idList
     * @return
     */
    @PostMapping(value = "/batchConfirmBill")
    @ApiOperation(value = "批量确认账单", notes = "批量确认账单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<Bill>> batchConfirmBill(@RequestBody List<Long> idList) {
        LOGGER.info("BillController.batchConfirmBill Param {} ", idList);
        RestfulResponse<List<Bill>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            billService.batchConfirmBill(idList);
        } catch (BaseException e) {
            LOGGER.error("BillController.batchConfirmBill ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("BillController.batchConfirmBill ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    /**
     * 批量删除账单
     *
     * @param idList
     * @return
     */
    @PostMapping(value = "/reset")
    @ApiOperation(value = "批量重置账单", notes = "批量删除账单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> reset(@RequestBody CommonVO<Long> idList) {
        LOGGER.info("BillController.reset param {} ", idList);
        RestfulResponse<String> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            if (Strings.isNullOrEmpty(idList.getTypeVal())) {
                throw new BaseException("重置方式异常");
            }
            int typeVal = idList.getTypeVal().equals("0") ? 0 : (idList.getTypeVal().equals("1") ? 1 : 3);
            billService.reset(idList.getEntityParams(), typeVal);
        } catch (BaseException e) {
            LOGGER.error("BillController.reset ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("BillController.reset ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    /**
     * 计算合同比例和金额
     *
     * @param bill
     * @return
     */
    @PostMapping(value = "/computeContractMoney")
    @ApiOperation(value = "计算账单的比例和金额", notes = "计算账单的比例和金额", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<BigDecimal> computeContractMoney(@RequestBody Bill bill) {
        LOGGER.info("BillController.computeContractMoney param {} ", bill);
        RestfulResponse<BigDecimal> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            if (bill == null || bill.getId() == null || bill.getGrossAmt() == null) {
                restful.setData(BigDecimal.ZERO);
                return restful;
            }
            restful.setData(billService.computeContractMoney(bill));
        } catch (BaseException e) {
            LOGGER.error("BillController.computeContractMoney error: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("BillController.computeContractMoney error: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    /**
     * 计算合同比例和金额
     *
     * @param billList
     * @return
     */
    @PostMapping(value = "/callBackDelBill")
    @ApiOperation(value = "撤销删除账单", notes = "撤销删除账单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> callBackDelBill(@RequestBody List<Bill> billList) {
        LOGGER.info("BillController.computeContractMoney param {} ", billList);
        RestfulResponse<Object> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            if (CollectionUtils.isEmpty(billList)) {
                restful.setMessage("请选择数据");
                restful.setCode(CommonEnum.ERROR.getCode());
                return restful;
            }
            billService.callBackDelBill(billList);
        } catch (BaseException e) {
            LOGGER.error("BillController.callBackDelBill error: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("BillController.callBackDelBill error: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    /**
     * 设置查询条件
     *
     * @param inParam
     */
    private void pageConditionSet(Page<Bill> inParam) {
        JwtAccountVO account = AccountUtil.getAccountInfoFromSecurityContext();
        if (account == null) {
            throw new BaseException("无登录信息");
        }
        List<String> valueList = baseCommonParameService.getBaseCommonValueByKey(BaseParamKeyEnum.PAY_BILL_VIEW_KEY.getKey());
        if (CollectionUtils.isEmpty(valueList) || !valueList.contains(account.getUsername())) {
            inParam.getCondition().put("createUser", account.getUsername());
        }
        // if (!UserCommon.userList.contains(account.getUsername())) {
        //     inParam.getCondition().put("createUser", account.getUsername());
        // }
    }
}

